# Maintainer: James Duley <jagduley gmail>
# Maintainer: Philippe Renon <philippe_renon yahoo.fr>

_realname=osgearth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=2.10.1
pkgrel=1
arch=('any')
license=('LGPL')
url="http://osgearth.org"
depends=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
         "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
         "${MINGW_PACKAGE_PREFIX}-osgQt"
         "${MINGW_PACKAGE_PREFIX}-osgQt-debug"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-poco"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-rocksdb"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')
source=("https://github.com/gwaldron/osgearth/archive/${_realname}-${pkgver}.tar.gz"
        compile-fix.patch)
sha256sums=('aec8e2d86e700c45aac5e12d352ab7e4011b40c34533c998da7abe18d297af4f'
            '6fb84f61043268800b305966711caed21c7b88ca0d5ab72b18aa825961c4210d')

prepare() {
  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/compile-fix.patch
}

build() {
  _pkgver=${_realname}-${pkgver} # How it's tagged in git

  export FREETYPE_DIR=${MINGW_PREFIX}

  for builddir in {Release,Debug}-${MINGW_CHOST}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    mkdir -p ${builddir}
    pushd ${builddir}
      [[ "${builddir%-${MINGW_CHOST}}" == "Release" ]] && {
        local _opts=""
      } || {
        local _opts=""
      }

      msg "Building ${builddir%-${MINGW_CHOST}} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -G"MSYS Makefiles" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${builddir%-${MINGW_CHOST}} \
        -DOSG_DIR=${MINGW_PREFIX} \
        -DOSGEARTH_QT_BUILD=ON \
        -DOSGEARTH_QT_BUILD_LEGACY_WIDGETS=ON \
        ${_opts} \
        ../${_realname}-${_pkgver}

      make
    popd
  done
}

package_release() {
  depends+=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph")
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"

  cd Release-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/osgearth
  cp -rf ${srcdir}/${_realname}-${_realname}-${pkgver}/data ${pkgdir}${MINGW_PREFIX}/share/osgearth
}

package_debug() {
  depends+=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  options=('staticlibs' '!strip')
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (debug) (mingw-w64)"

  cd Debug-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-osgearth() {
  package_release
}

package_mingw-w64-i686-osgearth-debug() {
  package_debug
}

package_mingw-w64-x86_64-osgearth() {
  package_release
}

package_mingw-w64-x86_64-osgearth-debug() {
  package_debug
}
